<?php
namespace app\common\model;

class Pays extends Base
{

    public static function Submit_Pay($userInfo, $data)
    {
        $self = new static();
        switch ($data['shop']) {
            case 'vip':
                switch ($data['shopid']) {
                    case 1:
                        $name = '秒赞包月会员';
                        break;
                    case 2:
                        $name = '秒赞包季会员';
                        break;
                    case 3:
                        $name = '秒赞双季会员';
                        break;
                    case 4:
                        $name = '秒赞年费会员';
                        break;
                }
                //判断站点是否开启商品促销
                if (config('sys_is_shop_vip_zk') == 1) {
                    //计算折扣后的价格
                    $price = config('sys_' . $data['shop'] . '_price_' . $data['shopid']);
                    $zk = config('sys_shop_' . $data['shop'] . '_zk_' . $data['shopid'] . '');
                    $res_money = round($price * $zk / 10, 2);
                } else {
                    $res_money = config('sys_' . $data['shop'] . '_price_' . $data['shopid']);
                }
                break;
            case 'pe':
                switch ($data['shopid']) {
                    case 1:
                        $name = '挂机配额一个';
                        break;
                    case 2:
                        $name = '挂机配额三个';
                        break;
                    case 3:
                        $name = '挂机配额五个';
                        break;
                    case 4:
                        $name = '挂机配额十个';
                        break;
                }
                //判断站点是否开启商品促销
                if (config('sys_is_shop_pe_zk') == 1) {
                    //计算折扣后的价格
                    $price = config('sys_' . $data['shop'] . '_price_' . $data['shopid']);
                    $zk = config('sys_shop_' . $data['shop'] . '_zk_' . $data['shopid'] . '');
                    $res_money = round($price * $zk / 10, 2);
                } else {
                    $res_money = config('sys_' . $data['shop'] . '_price_' . $data['shopid']);
                }
                break;
            case 'dl':
                $name = is_daili($data['shopid']);
                $res_money = config('sys_daili_price_'.$data['shopid'].'');
                break;
            case 'rmb':
                $name = $data['shopid'] . '元';
                $res_money = $data['shopid'];
                break;
            case 'fz':
                //计算分站开通价格
                $name = $data['webname'];
                //开通分站的域名
                $fzUrl = $data['qz'] . '.' . $data['domain'];
                if ($fzUrl == $_SERVER['HTTP_HOST']) {
                    $result = '<script>x.msg(\'你的网站域名不能跟主站一样\');</script>';
                    return $result;
                }elseif (Weblist::where('user_id', '=', $userInfo['uid'])->field('web_id')->find()){
                    $result = '<script>x.msg(\'你已经开通过网站了\');</script>';
                    return $result;
                }
                cookie('ktfz_url', $fzUrl);
                $res_money = config('sys_' . $data['shop'] . '_price_' . $data['shopid']);
                break;
        }
        $insert = [
            'uid' => $userInfo['uid'],
            'qq' => $userInfo['qq'],
            'orderid' => date("YmdHis") . rand(111, 999),
            'addtime' => date('Y-m-d H:i:s'),
            'name' => $name,
            'money' => $res_money,
            'type' => $data['pay_type'],
            'shop' => $data['shop'],
            'shopid' => $data['shopid'],
            'zid' => WEB_ID
        ];
        if ($self->insert($insert)) {
            $row = $self->where('uid', '=', $userInfo['uid'])->order('addtime desc')->find();
            if (config('sys_is_pay') == 1) {
                $pay_url = '/Code_Pay/submit?orderid=' . $row['orderid'] . '&type=' . $data['pay_type'] . '';
            } elseif (config('sys_is_pay') == 2) {
                $pay_url = '/Epay/submit?orderid=' . $row['orderid'] . '&type=' . $data['pay_type'] . '';
            }
            $result = '<script>window.location.href=\''.$pay_url.'\';</script>';
        }
        return $result;
    }
	
    public
    static function findByOrderId($orderid)
    {
        $self = new static();
        if ($result = $self->where('orderid', '=', $orderid)->find()) {
            return $result;
        } else {
            return false;
        }
    }

    public
    static function updateByOrderId($orderid, $data)
    {
        $self = new static();
        if ($result = $self->where('orderid', '=', $orderid)->update($data)) {
            return $result;
        } else {
            return false;
        }
    }
}